from flask import render_template, g, abort, request, jsonify, current_app

from info import db
from info.common import user_login_data
from info.constants import USER_COLLECTION_MAX_NEWS, QINIU_DOMIN_PREFIX
from info.models import tb_user_collection, Category, News
from info.modules.user import user_blu
from info.utils.image_storage import upload_img
from info.utils.response_code import RET, error_map


@user_blu.route("/user_info")
@user_login_data
def user_info():
    user = g.user
    if user:
        return render_template("news/user.html", user=user.to_dict())

    return abort(404)


@user_blu.route("/base_info", methods=["GET", "POST"])
@user_login_data
def user_base_info():
    user = g.user
    if user:
        if request.method == "GET":
            return render_template("news/user_base_info.html", user=user.to_dict())

        signature = request.json.get("signature")
        nick_name = request.json.get("nick_name")
        gender = request.json.get("gender")

        if not all([signature, nick_name, gender]):
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        if gender not in ["MAN", "WOMAN"]:
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        user.signature = signature
        user.nick_name = nick_name
        user.gender = gender

        db.session.commit()
        return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])

    return abort(404)


@user_blu.route('/pic_info', methods=['GET', 'POST'])
@user_login_data
def pic_info():
    user = g.user
    if user:
        if request.method == "GET":
            return render_template("news/user_pic_info.html", user=user.to_dict())

        try:
            img_byte = request.files.get("avatar").read()
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        try:
            file_name = upload_img(img_byte)
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR])

        user.avatar_url = file_name
        return jsonify(errno=RET.OK, errmsg=error_map[RET.OK], data=user.to_dict())

    return abort(404)


@user_blu.route("pass_info", methods=["GET", "POST"])
@user_login_data
def pass_info():
    user = g.user
    if user:
        if request.method == "GET":
            return render_template("news/user_pass_info.html", user=user.to_dict())
        old_password = request.json.get("old_password")
        new_password = request.json.get("new_password")

        if not all([old_password, new_password]):
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        if not user.check_password(old_password):
            return jsonify(errno=RET.PWDERR, errmsg=error_map[RET.PWDERR])

        user.password = new_password
        return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])

    return abort(404)


@user_blu.route('/collection')
@user_login_data
def collection():
    user = g.user
    if user:
        page = request.args.get("p", 1)

        try:
            page = int(page)
        except BaseException as e:
            current_app.logger.error(e)
            page = 1

        news_list = []
        total_page = 1

        try:
            pn = user.collection_news.order_by(tb_user_collection.c.create_time.desc()).paginate(page, USER_COLLECTION_MAX_NEWS)
            news_list = pn.items
            cur_page = page
            total_page = pn.pages
        except BaseException as e:
            current_app.logger.error(e)

        data = {
            "news_list":[news.to_dict() for news in news_list],
            "cur_page":page,
            "total_page":total_page
        }

        return render_template("news/user_collection.html", data=data)


@user_blu.route("/news_release", methods=["GET", "POST"])
@user_login_data
def news_release():
    user = g.user
    if user:
        if request.method == "GET":
            categories = []
            try:
                categories = Category.query.all()
            except BaseException as e:
                current_app.logger.error(e)

            if len(categories):
                categories.pop(0)

            return render_template("news/user_news_release.html", categories=categories)

        title =  request.form.get("title")
        category_id =  request.form.get("category_id")
        digest =  request.form.get("digest")
        index_image =  request.files.get("index_image")
        content =  request.form.get("content")

        if not all([title, category_id, digest, index_image, content]):
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        try:
            category_id = int(category_id)
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        news = News()
        news.title = title
        news.category_id = category_id
        news.digest = digest
        news.content = content

        try:
            file_image = index_image.read()
            image_name = upload_img(file_image)
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR])

        news.index_image_url = QINIU_DOMIN_PREFIX + image_name
        news.source = "个人发布"
        news.user_id = user.id
        news.status = 1

        db.session.add(news)

        return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])

    return abort(404)


@user_blu.route('/news_list')
@user_login_data
def news_list():
    user = g.user
    if user:
        page = request.args.get("p", 1)

        try:
            page = int(page)
        except BaseException as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR])

        news_list = []
        total_page = 1

        try:
            pn = user.news_list.order_by(News.create_time.desc()).paginate(page, USER_COLLECTION_MAX_NEWS)
            news_list = pn.items
            cur_page = page
            total_page = pn.pages
        except BaseException as e:
            current_app.logger.error(e)

        data = {
            "news_list" : news_list,
            "cur_page" : page,
            "total_page" : total_page
        }

        return render_template("news/user_news_list.html", data=data)

    return abort(404)
